/*
 * @Author: huangwei
 * @Date: 2021-05-17 10:47:59
 * @LastEditors: 黄威
 * @LastEditTime: 2022-02-09 11:01:00
 * @Description: 添加地图服务相关方法和接口
 */
import * as Cesium from 'Cesium'

import { Component } from 'vue-property-decorator'
import { createService3d } from '@/utils/service-creator'

import BaseLayerMixin from '../BindMap/BaseLayerMixin'

// 修改透明度和可见性方法
function setOpacity(layObj: any, dOpa: number) {
    ;(layObj as Cesium.ImageryLayer).alpha = dOpa
}
function setVisable(layObj: any, isVisable: boolean) {
    ;(layObj as Cesium.ImageryLayer).show = isVisable
}

@Component
export default class LayerMixin extends BaseLayerMixin<
    Cesium.Viewer,
    Cesium.ImageryLayer
> {
    adapter = {
        createLayer(
            svcType: string,
            svcUrl: string,
            props?: any
        ): Promise<Cesium.ImageryLayer> {
            return createService3d(svcType, svcUrl, props)
        },

        addToMap(m: Cesium.Viewer, l: Cesium.ImageryLayer): void {
            m.imageryLayers.add(l)
        },
        removeFromMap(m: Cesium.Viewer, l: Cesium.ImageryLayer): void {
            m.imageryLayers.remove(l)
        },

        moveLayer(
            m: Cesium.Viewer,
            l: Cesium.ImageryLayer,
            l2?: Cesium.ImageryLayer
        ) {
            const collection = m.imageryLayers

            if (!l2) collection.lowerToBottom(l)
            else {
                const layers = (collection as any)
                    ._layers as Cesium.ImageryLayer[]

                // 删除之前的图层
                layers.splice(collection.indexOf(l), 1)
                // 插入到指定图层的后面
                layers.splice(collection.indexOf(l2) + 1, 0, l)

                // 派发事件
                ;(collection as any)._update()
                collection.layerMoved.raiseEvent(
                    l,
                    collection.indexOf(l),
                    collection.indexOf(l2)
                )
            }
        },

        setOpacity(l: Cesium.ImageryLayer, dOpa: number): void {
            setOpacity(l, dOpa)
        },
        setVisable(l: Cesium.ImageryLayer, isVisable: boolean): void {
            setVisable(l, isVisable)
        },
    }
}
